//
//  YXToolViewHandler.m
//  YXImageBrowserDemo
//
//  Created by guo peng huang on 2021/5/18.
//

#import "YXToolViewHandler.h"
#import "YXCopywriter.h"
#import "YXUtilities.h"

@interface YXToolViewHandler ()
@property (nonatomic, strong) YXSheetView *sheetView;
@property (nonatomic, strong) YXSheetAction *saveAction;
@property (nonatomic, strong) YXTopView *topView;
@end

@implementation YXToolViewHandler

#pragma mark - <YXToolViewHandler>

@synthesize yx_containerView = _yx_containerView;
@synthesize yx_containerSize = _yx_containerSize;
@synthesize yx_currentPage = _yx_currentPage;
@synthesize yx_totalPage = _yx_totalPage;
@synthesize yx_currentOrientation = _yx_currentOrientation;
@synthesize yx_currentData = _yx_currentData;

- (void)yx_containerViewIsReadied {
    [self.yx_containerView addSubview:self.topView];
    [self layoutWithExpectOrientation:self.yx_currentOrientation()];
}

- (void)yx_pageChanged {
    if (self.topView.operationType == YXTopViewOperationTypeSave) {
        self.topView.operationButton.hidden = [self currentDataShouldHideSaveButton];
    }
    [self.topView setPage:self.yx_currentPage() totalPage:self.yx_totalPage()];
}

- (void)yx_respondsToLongPress {
    [self showSheetView];
}

- (void)yx_hide:(BOOL)hide {
    self.topView.hidden = hide;
    [self.sheetView hideWithAnimation:NO];
}



- (void)yx_orientationWillChangeWithExpectOrientation:(UIDeviceOrientation)orientation {
    [self.sheetView hideWithAnimation:NO];
}

- (void)yx_orientationChangeAnimationWithExpectOrientation:(UIDeviceOrientation)orientation {
    [self layoutWithExpectOrientation:orientation];
}

#pragma mark - private

- (BOOL)currentDataShouldHideSaveButton {
    id<YXDataProtocol> data = self.yx_currentData();
    BOOL allow = [data respondsToSelector:@selector(yx_allowSaveToPhotoAlbum)] && [data yx_allowSaveToPhotoAlbum];
    BOOL can = [data respondsToSelector:@selector(yx_saveToPhotoAlbum)];
    return !(allow && can);
}

- (void)layoutWithExpectOrientation:(UIDeviceOrientation)orientation {
    CGSize containerSize = self.yx_containerSize(orientation);
    UIEdgeInsets padding = YXPaddingByBrowserOrientation(orientation);
    
    self.topView.frame = CGRectMake(padding.left, padding.top, containerSize.width - padding.left - padding.right, [YXTopView defaultHeight]);
}

- (void)showSheetView {
    if ([self currentDataShouldHideSaveButton]) {
        [self.sheetView.actions removeObject:self.saveAction];
    } else {
        if (![self.sheetView.actions containsObject:self.saveAction]) {
            [self.sheetView.actions addObject:self.saveAction];
        }
    }
    [self.sheetView showToView:self.yx_containerView orientation:self.yx_currentOrientation()];
}

#pragma mark - getters

- (YXSheetView *)sheetView {
    if (!_sheetView) {
        _sheetView = [YXSheetView new];
        __weak typeof(self) wSelf = self;
        [_sheetView setCurrentdata:^id<YXDataProtocol>{
            __strong typeof(wSelf) self = wSelf;
            if (!self) return nil;
            return self.yx_currentData();
        }];
    }
    return _sheetView;
}

- (YXSheetAction *)saveAction {
    if (!_saveAction) {
        __weak typeof(self) wSelf = self;
        _saveAction = [YXSheetAction actionWithName:[YXCopywriter sharedCopywriter].saveToPhotoAlbum action:^(id<YXDataProtocol> data) {
            __strong typeof(wSelf) self = wSelf;
            if (!self) return;
            if ([data respondsToSelector:@selector(yx_saveToPhotoAlbum)]) {
                [data yx_saveToPhotoAlbum];
            }
            [self.sheetView hideWithAnimation:YES];
        }];
    }
    return _saveAction;
}

- (YXTopView *)topView {
    if (!_topView) {
        _topView = [YXTopView new];
        _topView.operationType = YXTopViewOperationTypeMore;
        __weak typeof(self) wSelf = self;
        [_topView setClickOperation:^(YXTopViewOperationType type) {
            __strong typeof(wSelf) self = wSelf;
            if (!self) return;
            switch (type) {
                case YXTopViewOperationTypeSave: {
                    id<YXDataProtocol> data = self.yx_currentData();
                    if ([data respondsToSelector:@selector(yx_saveToPhotoAlbum)]) {
                        [data yx_saveToPhotoAlbum];
                    }
                }
                    break;
                case YXTopViewOperationTypeMore: {
                    [self showSheetView];
                }
                    break;
                default:
                    break;
            }
        }];
    }
    return _topView;
}

@end
